/**
 * All rights Reserved, Designed By junglead.cn
 * @package cn.junglead.common.utils
 * @title SecurityUtil.java
 * @date 2020年8月30日
 */
package cn.junglead.common.utils;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * 过时的工具，准备移除。
 * @author Gary.Wang
 * @version 1.0
 */
@Deprecated
public final class SecurityUtil {
	
	// 默认算法
	private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";
	
	/**
	 * private 1:明文转换为加密数组
	 * @param key
	 * @param plaintext
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws NoSuchPaddingException
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws UnsupportedEncodingException
	 */
	private static byte[] aesEncryptToBytes(String key, String plaintext) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {  
        KeyGenerator kgen = KeyGenerator.getInstance("AES");  
        kgen.init(128);  
        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);  
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"));  
        return cipher.doFinal(plaintext.getBytes("utf-8"));  
    }
	/**
	 * private 2:从密文解密
	 * @param encryptBytes
	 * @param decryptKey
	 * @return
	 * @throws Exception
	 */
	private static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {  
        KeyGenerator kgen = KeyGenerator.getInstance("AES");  
        kgen.init(128);
        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);  
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));  
        byte[] decryptBytes = cipher.doFinal(encryptBytes);  
        return new String(decryptBytes);  
    }
	/**
	 * private 3:base64转码
	 * @param ciphertext
	 * @return
	 * @throws Exception
	 */
	private static byte[] base64Decode(String ciphertext) throws Exception{
        return Base64.getDecoder().decode(ciphertext);
    }
	/**
	 * private 4:base64转码
	 * @param bytes
	 * @return
	 */
	private static String base64Encode(byte[] bytes){  
        return org.apache.commons.codec.binary.Base64.encodeBase64String(bytes);  
    } 
	
	
	


	/**
	 * 1 MD5摘要式加密
	 * @param plaintext 原始明文
	 * @return 返回摘要密文
	 */
	public static String encryptMD5(String plaintext){
		return DigestUtils.md5Hex(plaintext);
	}
	
	/**
	 * 2.1 SHA1单向加密
	 * @param plaintext 原始明文
	 * @return 返回加密后的密文
	 */
	public static String encryptSHA1(String plaintext){
		return DigestUtils.sha1Hex(plaintext);
	}
	
	/**
	 * 2.2 SHA256单向加密
	 * @param plaintext 原始明文
	 * @return 返回加密后的密文
	 */
	public static String encryptSHA256(String plaintext){
		return DigestUtils.sha256Hex(plaintext);
	}
	
	/**
	 * 3.1 BASE64编码
	 * @param plaintext 原始明文
	 * @return 返回编码后的内容
	 */
	public static String encryptBASE64(String plaintext){
		return Base64.getEncoder().encodeToString(plaintext.getBytes());
	}
	/**
	 * 3.2 BASE64解码
	 * @param ciphertext 编码后的密文
	 * @return 返回解码后的明文
	 */
	public static String decryptBASE64(String ciphertext){
		byte[] decode = Base64.getDecoder().decode(ciphertext);
		return new String(decode);
	}
	
	/**
	 * 4.1 AES加密
	 * @param key 秘钥
	 * @param plaintext 明文
	 * @return 返回加密后的密文
	 */
	public static String encryptAES(String key, String plaintext) {		
        try {
			return base64Encode(aesEncryptToBytes(key, plaintext));
		} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException
				| BadPaddingException | UnsupportedEncodingException e) {
			return "";
		}  
    }
	/**
	 * 4.2 AES 解密
	 * @param key 秘钥
	 * @param ciphertext 密文
	 * @return 返回解密后的明文
	 */
	public static String decryptAES(String key, String ciphertext) {
		try {
			return aesDecryptByBytes(base64Decode(ciphertext), key);
		} catch (Exception e) {
			return "";
		}
    }
}
